clear all
set more off
set scheme s2color

// Evolution of population gap over time (esp. before-after the 1938 reform)

global input "...\replication\data"
global output "...\replication\outputs"

use "$input\historical_population.dta"

ren CODGEO com

****************** Get dist border ******************
merge 1:1 com using "$input\com_arcgis.dta"
drop if _merge != 3
drop _merge
** Drop those within 30km of coast or national border
keep if dist_nat_border < 0 
keep if dist_river < 0


****************** Costmetics ******************
** Remove Corsica
tostring com, replace
drop if substr(com,2,1) == "A"
drop if substr(com,2,1) == "B"
destring com, replace


****************** Get neighbor region for all com ******************
tostring com, replace
replace com = "0"+com if strlen(com) == 4
merge 1:1 com using "$input\loc_i_raw.dta"
drop if _merge == 2
drop _merge
destring com, replace


********* Other side ****************
geonear com x_com y_com using "$input\loc_j_raw.dta" , neighbors(dep x_dep y_dep) nearcount(2)
gen other_dep = nid1 if nid1 != DEP
replace other_dep = nid2 if nid1 == DEP

egen home_lab_mkt = sum(pop_2018), by(DEP)
egen other_lab_mkt = sum(pop_2018), by(other_dep)


****************** Gen treatment ******************

replace dist_dep = -dist_dep if other_lab_mkt > home_lab_mkt

gen treat = (other_lab_mkt < home_lab_mkt)

** Focus on larger labor markets (results hold without, but are more noisy)
gen sample = 1 if other_lab_mkt > 50000 & treat == 0
replace sample = 1 if home_lab_mkt > 50000 & treat == 1
keep if sample == 1

****************** calonico et al ******************
foreach i in pop_1876 pop_1881 pop_1886 pop_1891 pop_1896 pop_1901 pop_1906 pop_1911 pop_1921 pop_1926 pop_1931 pop_1936 pop_1954 pop_1962 pop_1968 pop_1975 pop_1982 pop_1990 pop_1999 pop_2006 pop_2010 pop_2015 pop_2018 {
rdrobust `i' dist_dep , c(0) h(3) 
est store ATE_`i'
}

coefplot ATE_pop_1876 || ATE_pop_1881 || ATE_pop_1886 || ATE_pop_1891 || ATE_pop_1896 || ATE_pop_1901 || ATE_pop_1906 || ATE_pop_1911 || ATE_pop_1921 || ATE_pop_1926 || ATE_pop_1931 || ATE_pop_1936 || ATE_pop_1954 || ATE_pop_1962 || ATE_pop_1968 || ATE_pop_1975 || ATE_pop_1982 || ATE_pop_1990 || ATE_pop_1999 || ATE_pop_2006 || ATE_pop_2010 || ATE_pop_2015 || ATE_pop_2018, color(black) ciopts(recast(. rcap) color(black black) lwidth(*0.75 *1.5)) ///
vertical omitted  yline(0, lcolor(gray)) levels(95 90) ///
bycoefs  ///
xline(12.5, lcolor(red) lpattern(dash))  ///
xlabel(1 "1876" 2 "1881" 3 "1886" 4 "1891" 5 "1896" 6 "1901" 7 "1906" 8 "1911" 9 "1921" 10 "1926" 11 "1931" 12 "1936" 13 "1954" 14 "1962" 15 "1968" 16 "1975" 17 "1982" 18 "1990" 19 "1999" 20 "2006" 21 "2010" 22 "2015" 23 "2018", angle(45)) ///
ms(m) color(black)  graphregion(color(white)) bgcolor(white) ///
msize(small) ytitle("Excess residential mass" "on side with larger labor market") ///
title("") xtitle("Year") legend(off)  text(-750 17.7 "1938: Departmental road reform", box fcolor(white))
graph export "$output\figure_rdd_hist_pop.pdf", replace



